//
//  WPReadingCollectionViewCell.m
//  wobingwoyi
//
//  Created by WGP on 16/7/30.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPReadingFirstCollectionViewCell.h"
#import "WPReadingIndivalCaseCell.h"
#import "WPNewReadingIndivalCaseCell.h"

@interface WPReadingFirstCollectionViewCell ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UIView *noNetWorkView;
@property(nonatomic,assign)NSInteger page;

@end
@implementation WPReadingFirstCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = GlobalBgColor;
        [self noNetWorkView];
        
        self.page = 1;
        
        self.tableView = [[UITableView alloc] init];
        self.tableView.backgroundColor = GlobalBgColor;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        self.tableView.rowHeight = UITableViewAutomaticDimension;
        self.tableView.estimatedRowHeight = 200;
        
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 5)];
        [self.tableView registerClass:[WPReadingIndivalCaseCell class] forCellReuseIdentifier:@"indivalCase"];
        [self.tableView registerClass:[WPNewReadingIndivalCaseCell class] forCellReuseIdentifier:@"WPNewReadingIndivalCaseCell"];
        [self.contentView addSubview:self.tableView];
        
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
        }];
        
        ReachabilityStatus status = [[RealReachability sharedInstance] currentReachabilityStatus];
        if (status == RealStatusNotReachable)
        {
            self.tableView.hidden = YES;
        }
        
    }
    return self;
}

-(UIView *)noNetWorkView
{
    if (_noNetWorkView == nil)
    {
        _noNetWorkView = [[UIView alloc] init];
        _noNetWorkView.backgroundColor = GlobalBgColor;
        [self.contentView addSubview:_noNetWorkView];
        
        [_noNetWorkView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
        }];
        
        UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"noNetWork"]];
        [iconView sizeToFit];
        [_noNetWorkView addSubview:iconView];
        [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_noNetWorkView);
            make.top.equalTo(_noNetWorkView).offset(65);
        }];
        
        UILabel *tipLabel = [UILabel new];
        tipLabel.textColor = GloballightFontColor;
        tipLabel.font = [UIFont systemFontOfSize:14];
        tipLabel.text = @"网络请求失败,请检查您的网络设置";
        [_noNetWorkView addSubview:tipLabel];
        
        [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_noNetWorkView);
            make.top.equalTo(iconView.mas_bottom).offset(30);
        }];
        
        UIButton *loadAgainBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [loadAgainBtn setTitle:@"重新加载" forState:UIControlStateNormal];
        loadAgainBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [loadAgainBtn setTitleColor:GloballightFontColor forState:UIControlStateNormal];
        [loadAgainBtn setTitleColor:GlobalColor forState:UIControlStateHighlighted];
        [loadAgainBtn setBackgroundImage:[UIImage imageNamed:@"loadAgain_normal"] forState:UIControlStateNormal];
        [loadAgainBtn setBackgroundImage:[UIImage imageNamed:@"loadAgain_highlight"] forState:UIControlStateHighlighted];
        
        [loadAgainBtn addTarget:self action:@selector(loadAgainBtnDidClick:) forControlEvents:UIControlEventTouchDown];
        [_noNetWorkView addSubview:loadAgainBtn];
        
        [loadAgainBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(105);
            make.height.mas_equalTo(31);
            make.centerX.equalTo(_noNetWorkView);
            make.top.equalTo(tipLabel.mas_bottom).offset(16);
        }];
        
    }
    return _noNetWorkView;
}

-(void)setModels:(NSMutableArray *)models
{
    _models = models;
    if (models.count < 10)
    {
        self.tableView.mj_footer.hidden = YES;
    }
    else
    {
        self.tableView.mj_footer.hidden = NO;
    }
    
    [self.tableView reloadData];
}

#pragma mark - tableView的数据源代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.models.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WPNewReadingIndivalCaseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WPNewReadingIndivalCaseCell"];
//    cell.model = self.models[indexPath.item];
    return cell;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(invidialCaseCellDidClickWith:model:)])
    {
        
        [self.delegate invidialCaseCellDidClickWith:self model:self.models[indexPath.item]];
    }
}

-(void)loadAgainBtnDidClick:(UIButton *)btn
{
    ReachabilityStatus status = [[RealReachability sharedInstance] currentReachabilityStatus];
    if (status != RealStatusNotReachable)
    {
        self.tableView.hidden = NO;
        if (self.loadAgainBlock)
        {
            self.loadAgainBlock();
        }
    }
}

@end
